package com.zjh.photoshopstudy.app.history;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zjh.photoshopstudy.Model.HistoryEntity;
import com.zjh.photoshopstudy.R;
import com.zjh.photoshopstudy.Utils.SharePreferencesUtils;

import java.util.ArrayList;
import java.util.List;

public class HistoryActivity extends AppCompatActivity implements onItemCheck<HistoryEntity> {
    HistoryAdapter adapter;
    TextView selectBtn;
    SelectBtnClickListener selectBtnClickListener;
    LinearLayout deleteLayout;
    TextView textView;
    List<HistoryEntity> historyList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        initView();
    }

    private void initView() {
        RecyclerView recyclerView = findViewById(R.id.historyList);
        List<HistoryEntity> list = SharePreferencesUtils.getHistory(this);
        selectBtn = findViewById(R.id.select_btn);
        deleteLayout = findViewById(R.id.layout);
        textView = findViewById(R.id.no_history_tip);
        adapter = new HistoryAdapter(list, this, this);
        if (list != null && list.size() > 0) {
            recyclerView.setAdapter(adapter);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
        } else {
            textView.setVisibility(View.VISIBLE);
            selectBtn.setVisibility(View.INVISIBLE);
        }
        selectBtnClickListener = new SelectBtnClickListener(adapter, selectBtn);
        selectBtn.setOnClickListener(selectBtnClickListener);
    }

    @Override
    public void onItemCheck(HistoryEntity entity) {
        historyList.add(entity);
        if (historyList.size() == adapter.getItemCount()) {
            selectBtnClickListener.isAllSelect = true;
            selectBtn.setText("全不选");
        }

    }

    @Override
    public void onItemCancelCheck(HistoryEntity entity) {
        historyList.remove(entity);
        if (historyList.size() != adapter.getItemCount()) {
            selectBtnClickListener.isAllSelect = false;
            if (selectBtnClickListener.isSelect) {
                selectBtn.setText("全选");
            } else {
                selectBtn.setText("管理");
            }

        }
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cancel:
                doCancel();
                break;
            case R.id.delete:
                doDelete();
                break;

        }
    }

    private void doCancel() {
        deleteLayout.setVisibility(View.INVISIBLE);
        selectBtnClickListener.isSelect = false;
        adapter.setSelect(false);
        historyList.clear();
        adapter.notifyDataSetChanged();
        selectBtn.setText("管理");
    }

    private void doDelete() {
        SharePreferencesUtils.deleteHistory(historyList, this);
        List<HistoryEntity> list = adapter.getList();
        list.removeAll(historyList);
        adapter.setList(list);
        adapter.notifyDataSetChanged();
        if (adapter.getItemCount() == 0) {
            selectBtn.setVisibility(View.INVISIBLE);
            textView.setVisibility(View.VISIBLE);
        }
        doCancel();
    }

    class SelectBtnClickListener implements OnClickListener {
        HistoryAdapter adapter;
        TextView selectBtn;
        boolean isSelect = false;
        boolean isAllSelect = false;

        public SelectBtnClickListener(HistoryAdapter adapter, TextView selectBtn) {
            this.adapter = adapter;
            this.selectBtn = selectBtn;
        }

        @Override
        public void onClick(View view) {
            if (isSelect) {
                historyList.clear();
                if (isAllSelect) {
                    adapter.setAllSelect(false);
                    selectBtn.setText("全选");
                } else {
                    adapter.setAllSelect(true);
                    selectBtn.setText("全不选");
                }
                isAllSelect = !isAllSelect;
                adapter.notifyDataSetChanged();
            } else {
                adapter.setSelect(true);
                adapter.notifyDataSetChanged();
                deleteLayout.setVisibility(View.VISIBLE);
                selectBtn.setText("全选");
                isSelect = true;
            }
        }
    }


}
